﻿using Commons.ReturnData;
using Microsoft.EntityFrameworkCore;
using OpenAI.Domain.Entities;
using OpenAI.Domain.IRepositories;
using System.Linq.Expressions;

namespace OpenAI.Infrastructure.Repositories
{
    public class ChatSessionRepository : IChatSessionRepository
    {
        private readonly ChatDbContext context;

        public ChatSessionRepository(ChatDbContext context)
        {
            this.context = context;
        }
        public async Task<ChatSession> GetChatSessionsByIdAsync(int id) => await context.ChatSessions.FirstOrDefaultAsync(x => x.Id == id);
        public async Task<DataResult<ChatSession>> GetChatSessionsByUserIdAsync(Guid appUserId)
        {
            var datas =  await context.ChatSessions.Where(x=>x.AppUserId == appUserId).ToListAsync();
            return new DataResult<ChatSession>(datas.Count, datas);
        }

        public async Task<DataResult<ChatSession>> GetChatSessionsByUserIdAsync(int pageIndex,int pageSize,Expression<Func<ChatSession,bool>> expressions)
        {
            var datas = new List<ChatSession>();
            var result = new DataResult<ChatSession>();
            if (expressions != null)
            {
                datas = await context.ChatSessions.Where(expressions).ToListAsync();
                result.Total = datas.Count;
            }
            if(pageSize < 1)
            {
                pageSize = 100;
            }
            datas = datas.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Datas = datas;
            return result;

        }


        public async Task<MessageResult> CreateChatSessionAsync(Guid userId, string title)
        {
            var chatSession = new ChatSession(userId,title);
            await context.ChatSessions.AddAsync(chatSession);
            try
            {
                await context.SaveChangesAsync();
                return new MessageResult("创建成功!");
            }
            catch (Exception ex)
            {

                return new MessageResult(500,"创建失败!",false);
            }
        }

        public async Task<MessageResult> DeletedChatSessionAsync(int id)
        {
            var chatSession = await GetChatSessionsByIdAsync(id);
            chatSession.SoftDelete();
            var res = await UpdateChatSessionAsync(chatSession);
            if (res.Successed){
                return new MessageResult("删除成功!");
            }
            else
            {
                return new MessageResult(500, "删除失败!", false);
            }
        }



        public async Task<MessageResult> UpdateChatSessionAsync(ChatSession chatSession)
        {
            try
            {
                context.ChatSessions.Update(chatSession);
                await context.SaveChangesAsync();
                return new MessageResult("编辑成功!");

            }
            catch (Exception ex)
            {

                return new MessageResult(500, "编辑失败!", false);
            }
        }
    }
}
